package net.wanho.personalOffice.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.personalOffice.dao.IndividualTaskDAO;
import net.wanho.personalOffice.po.IndividualTask;
import net.wanho.systemSettings.po.Employee;

import java.sql.Timestamp;
import java.util.List;

public class IndividualTaskDAOImpl extends BaseDAOImpl<IndividualTask> implements IndividualTaskDAO {
    @Override
    public List<IndividualTask> selectAll() throws DAOException {
        try {
            String sql ="SELECT\n" +
                    "  i.TASK_ID taskId,\n" +
                    "  i.TOPIC topic,\n" +
                    "  i.DESCRIPTION description,\n" +
                    "  i.END_TIME endTime,\n" +
                    "  i.PRIORITY priority,\n" +
                    "  i.STATUS status,\n" +
                    "  i.CREATE_TIME createTime,\n" +
                    "  i.LINK_URL linkUrl,\n" +
                    "  i.TASK_TYPE taskType,\n" +
                    "  e1.EMPLOYEE_ID \"responsiblePerson.employeeId\",\n" +
                    "  e1.EMPLOYEE_NAME \"responsiblePerson.employeeName\",\n" +
                    "  e1.DEPARTMENT_ID \"responsiblePerson.departmentId\",\n" +
                    "  e1.POSITON_ID \"responsiblePerson.positionId\",\n" +
                    "  e1.PHONE \"responsiblePerson.phone\",\n" +
                    "  e1.EMAIL \"responsiblePerson.email\",\n" +
                    "  e1.STATUS \"responsiblePerson.status\",\n" +
                    "  e1.PARENT_EMPLOYEE_ID \"responsiblePerson.parentEmployeeId\",\n" +
                    "  e1.CREATE_TIME \"responsiblePerson.createTime\",\n" +
                    "  e1.UPDATE_TIME \"responsiblePerson.updateTime\",\n" +
                    "  e2.EMPLOYEE_ID \"operator.employeeId\",\n" +
                    "  e2.EMPLOYEE_NAME \"operator.employeeName\",\n" +
                    "  e2.DEPARTMENT_ID \"operator.departmentId\",\n" +
                    "  e2.POSITON_ID \"operator.positionId\",\n" +
                    "  e2.PHONE \"operator.phone\",\n" +
                    "  e2.EMAIL \"operator.email\",\n" +
                    "  e2.STATUS \"operator.status\",\n" +
                    "  e2.PARENT_EMPLOYEE_ID \"operator.parentEmployeeId\",\n" +
                    "  e2.CREATE_TIME \"operator.createTime\",\n" +
                    "  e2.UPDATE_TIME \"operator.updateTime\"\n" +
                    "FROM individual_task i\n" +
                    "LEFT JOIN employee e1\n" +
                    "ON i.RESPONSIBLE_PERSON=e1.EMPLOYEE_ID\n" +
                    "LEFT JOIN employee e2\n" +
                    "ON i.OPERATOR=e2.EMPLOYEE_ID ";
            Object[] params = {};
            return super.queryForList(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean delete(String id) throws DAOException {
        try {
            String sql ="DELETE\n" +
                    "FROM individual_task\n" +
                    "WHERE TASK_ID = ?;";
            Object[] params = {id};
            return super.jdbcTemplate.delete(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean update(IndividualTask entity) throws DAOException {
        try {
            String sql ="UPDATE individual_task\n" +
                    "SET STATUS = ?\n" +
                    "WHERE TASK_ID = ?;";
            Object[] params = {entity.getTaskId(),entity.getStatus()};
            return super.jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public IndividualTask findById(String id) throws DAOException {
        try {
            String sql ="SELECT\n" +
                    "  i.TASK_ID taskId,\n" +
                    "  i.TOPIC topic,\n" +
                    "  i.DESCRIPTION description,\n" +
                    "  i.END_TIME endTime,\n" +
                    "  i.PRIORITY priority,\n" +
                    "  i.STATUS status,\n" +
                    "  i.CREATE_TIME createTime,\n" +
                    "  i.LINK_URL linkUrl,\n" +
                    "  i.TASK_TYPE taskType,\n" +
                    "  e1.EMPLOYEE_ID \"responsiblePerson.employeeId\",\n" +
                    "  e1.EMPLOYEE_NAME \"responsiblePerson.employeeName\",\n" +
                    "  e1.DEPARTMENT_ID \"responsiblePerson.departmentId\",\n" +
                    "  e1.POSITON_ID \"responsiblePerson.positionId\",\n" +
                    "  e1.PHONE \"responsiblePerson.phone\",\n" +
                    "  e1.EMAIL \"responsiblePerson.email\",\n" +
                    "  e1.STATUS \"responsiblePerson.status\",\n" +
                    "  e1.PARENT_EMPLOYEE_ID \"responsiblePerson.parentEmployeeId\",\n" +
                    "  e1.CREATE_TIME \"responsiblePerson.createTime\",\n" +
                    "  e1.UPDATE_TIME \"responsiblePerson.updateTime\",\n" +
                    "  e2.EMPLOYEE_ID \"operator.employeeId\",\n" +
                    "  e2.EMPLOYEE_NAME \"operator.employeeName\",\n" +
                    "  e2.DEPARTMENT_ID \"operator.departmentId\",\n" +
                    "  e2.POSITON_ID \"operator.positionId\",\n" +
                    "  e2.PHONE \"operator.phone\",\n" +
                    "  e2.EMAIL \"operator.email\",\n" +
                    "  e2.STATUS \"operator.status\",\n" +
                    "  e2.PARENT_EMPLOYEE_ID \"operator.parentEmployeeId\",\n" +
                    "  e2.CREATE_TIME \"operator.createTime\",\n" +
                    "  e2.UPDATE_TIME \"operator.updateTime\"\n" +
                    "FROM individual_task i\n" +
                    "LEFT JOIN employee e1\n" +
                    "ON i.RESPONSIBLE_PERSON=e1.EMPLOYEE_ID\n" +
                    "LEFT JOIN employee e2\n" +
                    "ON i.OPERATOR=e2.EMPLOYEE_ID " +
                    "WHERE i.TASK_ID=?";
            Object[] params = {id};
            return super.queryForObject(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<Employee> selectEmployee() throws DAOException {
        try {
            String sql ="SELECT\n" +
                    "  EMPLOYEE_ID \"employeeId\",\n" +
                    "  EMPLOYEE_NAME \"employeeName\",\n" +
                    "  DEPARTMENT_ID \"departmentId\",\n" +
                    "  POSITON_ID \"positionId\",\n" +
                    "  PHONE \"phone\",\n" +
                    "  EMAIL \"email\",\n" +
                    "  STATUS \"status\",\n" +
                    "  PARENT_EMPLOYEE_ID \"parentEmployeeId\",\n" +
                    "  CREATE_TIME \"createTime\",\n" +
                    "  UPDATE_TIME \"updateTime\"\n" +
                    "FROM CRM.employee";

            Object[] params = {};
            return (List<Employee>) super.jdbcTemplate.queryForList(sql,Employee.class,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }


    @Override
    public Integer insertGetId(IndividualTask entity) throws DAOException {
        try {
            String sql ="INSERT INTO individual_task\n" +
                    "            (TOPIC,\n" +
                    "             DESCRIPTION,\n" +
                    "             RESPONSIBLE_PERSON,\n" +
                    "             END_TIME,\n" +
                    "             PRIORITY,\n" +
                    "             STATUS,\n" +
                    "             CREATE_TIME,\n" +
                    "             OPERATOR\n)" +
                    "VALUES (?,?,?,?,?,?,?,?);";
            Object[] params = {
                    entity.getTopic(),
                    entity.getDescription(),
                    entity.getResponsiblePerson().getEmployeeId(),
                    new Timestamp(entity.getEndTime().getTime()),
                    entity.getPriority(),
                    entity.getStatus(),
                    new Timestamp( entity.getCreateTime().getTime()),
                    entity.getOperator().getEmployeeId()
            };
            return super.jdbcTemplate.insert(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }
}
